package com.appsky.drift.dialog;

import com.alibaba.fastjson.JSONObject;
import com.appsky.drift.R;
import com.appsky.drift.api.AppSkyApp;
import com.appsky.drift.api.JsonFactory;
import com.appsky.drift.api.Utils;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.DialogInterface.OnClickListener;
import android.content.SharedPreferences.Editor;
import android.widget.EditText;

public class EnterNickAndSexDialog extends AlertDialog.Builder {

	private final Context context;

	private EditText edit;

	private String oldNick;

	private AppSkyApp app = null;

	public EnterNickAndSexDialog(Context context) {
		super(context);
		this.context = context;
		app = (AppSkyApp) ((Activity) context).getApplication();
		oldNick = app.myInfo.getNick();

		initView();
		initButton();

	}

	/**
	 * 初始化Dialog的View
	 */
	private void initView() {
		edit = new EditText(context);
		if (Utils.isNullString(oldNick)) {
			edit.setHint(R.string.enter_nick_text);
		} else {
			edit.setHint(oldNick);
		}
		setIcon(R.drawable.ic_compose);
		setTitle(R.string.enter_nick_title);
		setView(edit);
	}

	/**
	 * 初始化Dialog的Button，
	 * 
	 * @param myInfo
	 *            用来设置用户的Nick
	 */
	private void initButton() {

		final int oldSex = app.myInfo.getSex();

		SharedPreferences globalPref = context.getSharedPreferences(
				Utils.PREF_GLOBAL, Context.MODE_PRIVATE);
		final Editor editor = globalPref.edit();
		String[] sexItem = { "女", "男", "保密" };
		setSingleChoiceItems(sexItem, oldSex, new OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				app.myInfo.setSex(which);
				editor.putInt(context.getString(R.string.pref_userSex), which);
				editor.commit();
			}
		});
		setPositiveButton(R.string.ok, new OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				String nick = edit.getText().toString();
				if (Utils.isNullString(nick)) {
					if (Utils.isNullString(oldNick)) { // 用户还没有昵称
						app.myInfo.setNick(context
								.getString(R.string.default_nick));
					}
				} else { // 用户更改了昵称
					app.myInfo.setNick(nick);
					editor.putString(context.getString(R.string.pref_userNick),
							nick);
					editor.commit();
				}
				if (oldSex != app.myInfo.getSex()
						|| !nick.equals(oldNick)) {
					JsonFactory jsonFactory = JsonFactory.getFactory();
					JSONObject json = jsonFactory.makeUpdateInfoJson(app.myInfo.getUserId(),
							app.myInfo.getNick(), app.myInfo.getSex());
					Intent intent = new Intent(Utils.ACTION_SEND_MESSAGE);
					intent.putExtra("json", json.toJSONString());
					context.sendBroadcast(intent);
				}
			}
		});
		setNegativeButton(R.string.cancel, new OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {

				if (oldNick == null) {
					app.myInfo.setNick(context
							.getString(R.string.default_nick));
				}
			}
		});
	}

}
